<?php

/**
 * ECSHOP 超级管理文件
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: yehuaixiao $
 * $Id: order.php 17219 2011-01-27 10:49:19Z yehuaixiao $
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
@ini_set('display_errors',        2);

/*------------------------------------------------------ */
//-- 订单查询
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'reset_freeplan')
{
    /* 清空红包数据 */
    $sql = "TRUNCATE TABLE " . $ecs->table('user_freeplan_bonus');
    $db->query($sql);
    /* 清空所有用户奖金 */
    // $sql = "UPDATE " . $ecs->table('user_freeplan') . " SET money = 0";
    $db->query($sql);
    /* 获取所有报名用户列表 */
    $sql = "SELECT user_id, addtime, exam_year FROM " . $ecs->table('user_freeplan');
    $user_list = $db->getAll($sql);
    foreach ($user_list as $key => $value) {
        $userArr[] = $value['user_id'];
        $value['endtime'] = strtotime(intval($value['exam_year']) . '-03-08 00:00:00');
        $userInfo[$value['user_id']] = $value;
    }
    $userStr = implode(',', $userArr);
    /* 获取所有有效订单 */
    $sql = "SELECT order_id, add_time, order_sn, user_id, goods_amount, shipping_fee, discount FROM " . $ecs->table('order_info') . " oi WHERE oi.user_id IN ({$userStr}) AND oi.order_status != 2 AND oi.order_status != 3 AND oi.order_status != 4 ORDER BY oi.add_time";
    $order_list = $db->getAll($sql);

    foreach ($order_list as $key => $value) {
        $orderList[$value['user_id']][] = $value;
    }
    unset($order_list);

    foreach ($orderList as $key => $value) {
        $user[$key]['money'] = 0;
        foreach ($value as $k => $v) {
            if ($userInfo[$key]['addtime'] <= $v['add_time'] && $v['add_time'] < $userInfo[$key]['endtime']) {
                $user[$key]['money'] += $v['shipping_fee'] + $v['goods_amount'] - $v['discount'];
            }
        }
    }

    foreach ($user as $key => $value) {
        $sql = "UPDATE " . $GLOBALS['ecs']->table('user_freeplan') . " SET money = '{$value['money']}' WHERE user_id = $key";
        $GLOBALS['db']->query($sql);
    }

    echo "<pre>";
    print_r($user);
    echo "</pre>";
    exit;

    foreach ($user_list as $key => $value) {
        /* 获取用户所有有效订单信息 */
        $sql = "SELECT * FROM " . $ecs->table('order_info') . " oi WHERE oi.add_time >= $value[addtime] AND oi.user_id = $value[user_id] AND oi.order_status != 2 AND oi.order_status != 3 AND oi.order_status != 4 ORDER BY oi.add_time";
        $order_list = $db->getAll($sql);
        if($order_list){
            foreach ($order_list as $k => $order) {
                reset_freeplan($order);
                if($order['to_buyer'] && $money=substr($order['to_buyer'],23)){
                    $data['money'] = -$money;
                    $data['user_id'] = $order['user_id'];
                    $data['extension_code'] = 'refund_' . $order['order_sn'];
                    $data['note'] = '退款订单号：' . $order['order_sn'];
                    $data['time'] = $order['add_time'];
                    if($money>0){
                        auto_receive_freeplan($data);// 奖金更新
                    }
                }
            }
        }
    }

    $link[] = array('href' => 'index.php?act=main', 'text' => '返回起始页');
    sys_msg('重置成功！', 0, $link);
}
/**
 * 订单发放奖金
 * $order  发放奖金信息
 * $status 修改状态，默认为1：发放奖金；2：退回奖金；3：修改发放奖金
 */
function reset_freeplan($order, $status='1'){
    $ceiling = 1000;
    $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('user_freeplan') ." WHERE user_id = $order[user_id]";
    $freeplan = $GLOBALS['db']->getRow($sql);
    if($freeplan){
        // 发放订单奖金
            if($freeplan['exam_year'] == '2015年'){
                $ceiling = 2000;
            }else{
                $ceiling = 1000;
            }
            $add_time = $order['add_time'];
            $start_time =strtotime(date('Y-m',$add_time ) . '-01 00:00:00');
            $end_time = strtotime(date('Y',$add_time ) . '-' . ((date('m',$add_time ) < 12) ? (date('m',$add_time )+1) : 1) . '-01 00:00:00');
            $sql = "SELECT SUM(money) AS sum FROM ".$GLOBALS['ecs']->table('user_freeplan_bonus') ." WHERE user_id = $order[user_id] AND add_time >= $start_time AND add_time < $end_time";
            $freeplan_money = $GLOBALS['db']->getOne($sql);
            $money = $order['goods_amount']+$order['shipping_fee']+$order['freeplan_money'];
            if($freeplan_money >= $ceiling){
                $money = 0;
                return false;
            }else{
                if($money >= $ceiling){
                    $money = $ceiling-$freeplan_money;
                }elseif($money+$freeplan_money>=$ceiling){
                    $money = $ceiling-$freeplan_money;
                }
                if($money <= 0) return false;
                $sql = "INSERT INTO " . $GLOBALS['ecs']->table('user_freeplan_bonus') . "(`user_id`,`money`,`add_time`,`extension_code`, `note`) VALUES('$order[user_id]','$money','$order[add_time]','$order[order_sn]', '发放$order[order_sn]奖金')";
                $GLOBALS['db']->query($sql);
                $sql = "UPDATE " . $GLOBALS['ecs']->table('user_freeplan') . " SET money = money + $money WHERE user_id = $order[user_id]";
                $GLOBALS['db']->query($sql);
                return true;
            }
    }
}


?>
